/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package efe.bean;

import java.io.Serializable;
import java.util.regex.*;

/**
 * Wartość przechowująca adres IP.
 * @author root
 */
public class EfeIPValue implements EfeValue, Serializable {

    private static final Pattern IPV4_PATTERN = 
        Pattern.compile(
                "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

    private String IP = "127.0.0.1";

    public boolean validate(String value) {
        if (!isIPv4Address(value)) return false;
        IP = value;
        return true;

    }

    public String display() {
        return IP;
    }

    public String getMessage() {
        return "To nie jest adres IP podany w formacie xxx.xxx.xxx.xxx";
    }

    private boolean isIPv4Address(final String input) {
        return IPV4_PATTERN.matcher(input).matches();
    }

    public String getValue() {
        return IP;
    }

}
